package com.ywqf_api.service.impl;

import com.ywqf_api.base.BaseException;
import com.ywqf_api.dao.HouseNeedDao;
import com.ywqf_api.dto.exc.ClientExcution;
import com.ywqf_api.dto.exc.ToLoginExcution;
import com.ywqf_api.dto.in.HouseTypeDto;
import com.ywqf_api.entity.*;
import com.ywqf_api.enums.ClientEnum;
import com.ywqf_api.enums.ToLoginEnum;
import com.ywqf_api.service.HouseNeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class HouseNeedServiceImpl implements HouseNeedService {
    @Autowired
    private HouseNeedDao houseNeedDao;

    @Override
    public Map findSellHouse() {
        List<HouseType> houseType = houseNeedDao.houseType();
        List<SellHouse> sellHousePrice = houseNeedDao.sellHousePrice();
        List<HouseDirection> houseDirection = houseNeedDao.houseDirection();
        List<HouseArea> houseArea = houseNeedDao.houseArea();
        List<HouseFitment> houseFitment = houseNeedDao.houseFitment();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("buyHouse", houseType);
        map.put("buyHousePrice", sellHousePrice);
        map.put("houseDirection", houseDirection);
        map.put("houseArea", houseArea);
        map.put("houseFitment", houseFitment);
        return map;
    }

    @Override
    public Map findRentHouse() {
        List<HouseType> houseType = houseNeedDao.houseType();
        List<RentHouse> rentHousePrice = houseNeedDao.rentHousePrice();
        List<HouseDirection> houseDirection = houseNeedDao.houseDirection();
        List<HouseArea> houseArea = houseNeedDao.houseArea();
        List<HouseFitment> houseFitment = houseNeedDao.houseFitment();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("buyHouse", houseType);
        map.put("sellHousePrice", rentHousePrice);
        map.put("houseDirection", houseDirection);
        map.put("houseArea", houseArea);
        map.put("houseFitment", houseFitment);

        return map;
    }

    @Override
    public int saveHouse(HouseTypeDto houseTypeDto) throws BaseException {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        System.out.println(val);
        houseTypeDto.setHouseNeedRentNum(val);
        int customerId = houseTypeDto.getCustomerId();
        CustomerId customerId1 = houseNeedDao.findCustomerId(customerId);
        houseTypeDto.setCustomerName(customerId1.getName());
        houseTypeDto.setPhone(customerId1.getTelephone());
        int saveHouse = houseNeedDao.addRentHouse(houseTypeDto);
        if (saveHouse > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int buyHouse(HouseTypeDto houseTypeDto) throws BaseException {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        System.out.println(val);
        houseTypeDto.setHouseNeedRentNum(val);
        int customerId = houseTypeDto.getCustomerId();
        CustomerId customerId1 = houseNeedDao.findCustomerId(customerId);
        houseTypeDto.setCustomerName(customerId1.getName());
        houseTypeDto.setPhone(customerId1.getTelephone());
        int buyHouse = houseNeedDao.addBuyHouse(houseTypeDto);
        if (buyHouse > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int updateEvaluate(int maintainId, String evalContent, int evalDegree) throws BaseException {
        int updateEvaluate = houseNeedDao.updateEvaluate(evalContent, maintainId, evalDegree);
        if (updateEvaluate > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public Map findHousekeepingCard(int cardId) {
        HouseKeepingCard findHousekeepingCard = houseNeedDao.findHousekeepingCard(cardId);
        if (findHousekeepingCard != null) {
            if (findHousekeepingCard.getAddTime().length() >= 19) {
                findHousekeepingCard.setAddTime(findHousekeepingCard.getAddTime().substring(0, 19));
            }
        }
        CardTime findCardTime = houseNeedDao.findCardTime(cardId);
        if (findCardTime != null) {
            if (findCardTime.getAddTime().length() >= 19) {
                findCardTime.setAddTime(findCardTime.getAddTime().substring(0, 19));
            }
        }
        String findRestTimes = houseNeedDao.findRestTimes(cardId);
        String aaa = findCardTime.getPayType();
        findHousekeepingCard.setPayType(aaa);
        List<Records> allRecords = houseNeedDao.allRecords(cardId);
        for (int i = 0; i < allRecords.size(); i++) {
            Records records = allRecords.get(i);
            if (records.getAddTime().length() >= 19) {
                records.setAddTime(records.getAddTime().substring(0, 19));
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("findHousekeepingCard", findHousekeepingCard);
        map.put("findRestTimes", findRestTimes);
        map.put("allRecords", allRecords);
        if (map == null || map.size() == 0) {
            return null;
        }
        return map;
    }

    @Override
    public List findCardList(int customerId) {
        List<CardList> findCardList = houseNeedDao.findCardList(customerId);
        for (int i = 0; i < findCardList.size(); i++) {
            CardList cardList = findCardList.get(i);
            if(cardList.getAddTime().length() >=19){
                cardList.setAddTime(cardList.getAddTime().substring(0, 19));
            }
        }
        return findCardList;
    }

//    @Override
//    public ClientExcution findRoleList() {
//        List<RoleList> findRoleList=houseNeedDao.findRoleList();
//        return new ClientExcution(ClientEnum.FIND_SUCCESS,findRoleList);
//    }
}
